package myQueue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Test {
    public static void main1(String[] args) {
//        MyQueue queue=new MyQueue();
//        queue.push(1);
//        queue.push(2);
//        queue.push(3);
//        queue.push(4);
//        queue.push(5);
//        queue.display();
//        System.out.println("=====================");
//
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
//
//        queue.display();

//        MyqueueArray queue=new MyqueueArray(3);
//        queue.push(1);
//        queue.push(2);
//        queue.push(3);
//        queue.push(4);

//        System.out.println(queue.peek());
//        System.out.println(queue.poll());
//        System.out.println("==========");
//        System.out.println(queue.peek());
//        System.out.println(queue.poll());
//        System.out.println("==========");
//        System.out.println(queue.peek());
//        System.out.println(queue.poll());
//        System.out.println("==========");
//        System.out.println(queue.rear());
//        Queue<Integer> queue1=new LinkedList<>();
//        queue1.isEmpty()

    }


    public static void main(String[] args) {
        MyStack stack=new MyStack();
        stack.push(1);
        stack.push(2);
       //stack.push(3);
       // stack.push(4);

        System.out.println(stack.top());
        System.out.println(stack.top());
       // System.out.println(stack.top());
        Stack<Integer> stack1=new Stack<>();
        //stack1.s
        Stack stack2=new Stack();
       // stack2.e

    }
}
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }

    public void push(int x) {
        //往不为空的那个队列中添加元素
        //当两个队列都为空时则规定往queue1中添加元素(也可queue2)
        if(queue1.isEmpty()&&queue2.isEmpty()){
            queue1.offer(x);
        }else if(queue1.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }
    }

    //弹出"栈顶元素"
    public int pop() {
        int a;
        if(queue1.isEmpty()){
            a=queue2.size();
            while(a-1>0){
                queue1.offer(queue2.poll());
                a--;
            }
            return queue2.poll();
        }else{
            a=queue1.size();
            while(a-1>0){
                queue2.offer(queue1.poll());
                a--;
            }
            return queue1.poll();
        }
    }

    public int top() {
        int a;
        int topEvement;
        if(queue1.isEmpty()){
            a=queue2.size();
            while(a-1>0){
                queue1.offer(queue2.poll());
                a--;
            }
            topEvement=queue2.peek();
            queue1.offer(queue2.poll());
            return topEvement;
        }else{
            a=queue1.size();
            while(a-1>0){
                queue2.offer(queue1.poll());
                a--;
            }
            topEvement=queue1.peek();
            queue2.offer(queue1.poll());
            return topEvement;
        }
    }

    //判断"栈"是否为空
    public boolean empty() {
        //当两个队列都为空时此时"栈"为空
        if(queue1.isEmpty()&&queue2.isEmpty()){
            return true;
        }
        return false;
    }
}